/* Copyright (C) 2012  	Mathieu Marleix <mathieu.marleix@aerian.fr>
 * 						Sébastien Dufay
 * 						Romain Noel
 * 						Nicolas Dennewald
 * 						Slim Aloui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.esiee.mobiliteam;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Classe affichant la description d'un item de la liste RSS.
 * @author MathieuMarleix 
 * @author Sébastien Dufay 
 * @author Romain Noel
 * @author Nicolas Dennewald
 * @author Slim Aloui
 */
public class ActivityShowDescription extends Activity {

	public String room = null,teacher = null,groups = null,date = null,hours = null;
	
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.showdescription);
		String content = null;
		Intent startingIntent = getIntent();

		if (startingIntent != null) {
			Bundle bundle = startingIntent
					.getBundleExtra("android.intent.extra.rssItem");
			if (bundle == null) {
				content = getString(R.string.empty_flux);
			} else {
				
				
				content = bundle.getString("title") + "\n\n" 
						+ getDate(bundle.getString("description")) + getHours(bundle.getString("description")) 
						+ getRoom(bundle.getString("description")) + getTeacher(bundle.getString("description")) 
						+ getGroups(bundle.getString("description")).replaceAll("<br[ ]?/>", "");
			}
		} else {
			content = getString(R.string.empty_flux);

		}

		TextView textView = (TextView) findViewById(R.id.content);
		textView.setText(content);

		Button backbutton = (Button) findViewById(R.id.back);

		backbutton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}

	/**
	 * Fonction d'expression reguliere permettant de renvoyer
	 * les partie ayant matchées.
	 * @param expression : la regex
	 * @param obj : une description de la categorie (heure, date, classe...)
	 * @param source : le string source.
	 * @return : le string matché.
	 */
	public static String matchedString(String expression, String obj,String source){
		String objet = "";
		Pattern p=Pattern.compile(expression);
		Matcher m=p.matcher(source);
		if(m.find()) objet += obj.toString() + ": \n" + m.group(1) + "\n";
		while(m.find()) 
			objet += m.group(1) + "\n";
		if(!objet.equals("")) objet += "\n";
		return objet;
	}
	/**
	 * Différents getters et setters associés aux catégories.
	 */
	public String getRoom(String description) {
		return matchedString("([0-9]{4,4}[+]?)(<br />|<br/>)",getString(R.string.room),description);
		}
	public void setRoom(String room) {
		this.room = room;
	}
	public String getTeacher(String description) {
		return matchedString("([A-Z]+ [A-Z].)(<br />|<br/>)",getString(R.string.teacher),description);
	}
	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}
	public String getGroups(String description) {
		return matchedString("([A-Z][A-Z0-9-]{1,}|<br[ ]?/>[0-9])<br[ ]?/>", getString(R.string.groups), description);
	}
	public void setGroups(String groups) {
		this.groups = groups;
	}
	public String getDate(String description) {
		return matchedString("([0-9]{1,2}/[0-9]{1,2}/[0-9]{4,4})",getString(R.string.date),description);
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getHours(String description) {
		return matchedString("([0-9]{1,2}h[0-9]{1,2} - [0-9]{1,2}h[0-9]{1,2})", getString(R.string.hours),description);
	}
	public void setHeure(String hours) {
		this.hours = hours;
	}
}
